# Importing Data
RUSSIA <- read_excel("C:/users/Biniam/Desktop/Documents/Academic/Thesis/Analysis Folder/Excel Files/Steel/TSA.xlsx",sheet = "Sheet1", range = "AB1:AB239")

# Checking the Imported Data
View(RUSSIA)
# Creating Time Series Data
RUSSIA_ts <- ts(RUSSIA, start=c(2000,1), end=c(2019,09), frequency=12)
# Viewing and Checking the Created Time Series Data
RUSSIA_ts
sum(is.na(RUSSIA_ts))
library(forecast)
RUSSIA_ts <- tsclean(RUSSIA_ts)
RUSSIA_ts

# Identification: Plotting the Time Series Data
plot(RUSSIA_ts, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.1, col = 2, col.main = 4, col.lab = 2, col.axis = 3, xlab = "Years", ylab = "Volume of Steel (in tons)", main = "Russia's Monthly Steel Production")

# Estimating the appropriate model
RUSSIA_ts_model <- auto.arima(RUSSIA_ts)
RUSSIA_ts_model

plot.ts(RUSSIA_ts_model$resid, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.75, col = 2, col.main = 4, col.lab = 2, col.axis = 4, xlab = "Years", ylab = "Residuals", main = "Residual Plot")
acf(RUSSIA_ts_model$residuals, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.75, col = 2, col.main = 4, col.lab = 1, col.axis = 4, xlab = "Lag", ylab = "ACF", main="ACF Residual")
pacf(RUSSIA_ts_model$residuals, main='ACF Residual')
Box.test(RUSSIA_ts_model$resid, lag=20, type="Ljung-Box")

# Forecasting
options(max.print=1000000)
RUSSIA_ts_forecast <- forecast (RUSSIA_ts_model, level=c(95), h=255)
plot(RUSSIA_ts_forecast, cex = 0.5, cex.main = 1.5, font.main = 4, font.lab = 2, cex.lab = 1.5, cex.axis = 0.1, col = 2, col.main = 4, col.lab = 2, col.axis = 3, xlab = "Years", ylab = " Volume of Steel Production (in tons)", main = "Steel Production Forecasting for Russia")
RUSSIA_ts_forecast             

# Exporting
write.table(RUSSIA_ts_forecast, file="/users/Biniam/Desktop/Documents/Academic/Thesis/Result Folder/TSA Results/Excel Files/From R/Steel/RUSSIA_TSA.csv", sep=",")
